package com.vil.controller;

import com.alibaba.fastjson.JSONObject;
import com.vil.common.ServerResponse;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("file")
public class UploadController {

    /**
     * 公共的文件上传控制层
     */
    private String path = "D:/uploadPic/";
    //图片上传
    @PostMapping("upload")
    @ResponseBody
    public ServerResponse upload(@RequestParam("admin_pic") MultipartFile admin_pic,
                                 @RequestParam(value = "login_code", required = false) String login_code) {
        JSONObject data = new JSONObject();
        if (!admin_pic.isEmpty()) {
            String originalFilename = admin_pic.getOriginalFilename();
            int filesize = 500000;
            System.out.println(admin_pic.getSize()+"dssdds");
            if (admin_pic.getSize()>filesize){
                return ServerResponse.buildErrorMsg("文件大小超过500kb");
            }
            String ext= FilenameUtils.getExtension(originalFilename);
            if(ext.equalsIgnoreCase("jpg") || ext.equalsIgnoreCase("png")
                    ||ext.equalsIgnoreCase("jpeg") || ext.equalsIgnoreCase("pneg")){
                String fileName = System.currentTimeMillis() + "_admin.jpg"; //生成身份证随机文件名
                File targetFile = new File(path, fileName);
                if (!targetFile.exists()) {
                    targetFile.mkdirs();
                }
                try {
                    admin_pic.transferTo(targetFile);
                    ServerResponse.buildSuccessMsg("上传成功");
                    String image_name = "/upload/" + fileName;
                    System.out.println("url:====" + image_name);
//                data.put("image_name", image_name);
                    return ServerResponse.buildSuccessData(image_name);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }else {
                return ServerResponse.buildErrorMsg("文件格式不符合要求");
            }
        }
        return ServerResponse.buildErrorMsg("上传文件不能为空");

    }

    //删去上传的图片
    @RequestMapping("deletePic.html")
    @ResponseBody
    public String deletePic(
            @RequestParam(value="picpath",required=false) String picpath,
            HttpSession session){
        System.out.println("页面获取的路径："+picpath);
        String[] paths = picpath.split("/"); //分割

        String path ="D://uploadPic/"+ paths[2];
        System.out.println("path:"+path);
        File file = new File(path);
        if(file.exists()){
            file.delete();
            return "success";
        }
        return "failed";
    }
}
